package com.example.dnfgamercommunitybackend.service.impl;

import com.example.dnfgamercommunitybackend.dao.ManageUserDao;
import com.example.dnfgamercommunitybackend.entity.Account;
import com.example.dnfgamercommunitybackend.entity.ResponseResult;
import com.example.dnfgamercommunitybackend.service.ManageUserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ManageUserServiceImpl implements ManageUserService {
    @Resource
    private ManageUserDao manageUserDao;
    @Override
    public String getUserList() {
        List<Account> accounts = manageUserDao.getUserList();
        if (accounts.isEmpty()){
            return ResponseResult.failureStringResult("响应失败,服务器故障");
        }
        return ResponseResult.successStringResult("响应成功",accounts);
    }

    @Override
    public String banUserByUserId(int userId) {
        if (String.valueOf(userId).isEmpty()){
            return ResponseResult.failureStringResult("操作失败，用户id为空");
        }
        int userStatus = manageUserDao.getUserStatus(userId);
        //如果账号已处于封禁状态，返回失败
        if (userStatus == 2){
            return ResponseResult.failureStringResult("账号已处于封禁状态");
        }
        if (manageUserDao.banUserByUserId(userId) > 0){
            return ResponseResult.successStringResult("操作成功");
        }else {
            return ResponseResult.failureStringResult("操作失败，用户id不存在");
        }
    }

    @Override
    public String allowUserByUserId(int userId) {
        if (String.valueOf(userId).isEmpty()){
            return ResponseResult.failureStringResult("操作失败，用户id为空");
        }
        int userStatus = manageUserDao.getUserStatus(userId);
        //如果账号已处于正常状态，返回失败
        if (userStatus == 1){
            return ResponseResult.failureStringResult("账号已处于正常状态");
        }
        if (manageUserDao.allowUserByUserId(userId) > 0){
            return ResponseResult.successStringResult("操作成功");
        }else {
            return ResponseResult.failureStringResult("操作失败，用户id不存在");
        }
    }

    @Override
    public String deleteUserById(int userId) {
        if (String.valueOf(userId).isEmpty()){
            return ResponseResult.failureStringResult("删除失败，用户id为空");
        }
        if (manageUserDao.deleteUserById(userId) > 0) {
            return ResponseResult.successStringResult("删除成功");
        }else {
            return ResponseResult.failureStringResult("删除失败，用户id不存在");
        }
    }

    @Override
    public String getUserByKeyword(String keyword) {
        if (StringUtils.hasText(keyword)){
            List<Account> accounts = manageUserDao.getUserByKeyword(keyword);
            if (!accounts.isEmpty()){
                return ResponseResult.successStringResult("查询成功",accounts);
            }
        }
        return ResponseResult.failureStringResult("查询失败,暂无相关用户");
    }
}
